﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace MishraReader
{
    public interface IWindowPositionTracker
    {
        void Start();
        void Stop();
    }

    public class WindowPositionTracker : IWindowPositionTracker
    {
        private readonly ISettingsManager _settingsManager;
        private readonly Window _window;

        public WindowPositionTracker(Window window, ISettingsManager settings)
        {
            _settingsManager = settings;
            _window = window;
        }

        public void Start()
        {
            RestorePosition();
            TrackWindowPosition();
        }

        public void Stop()
        {
            if (_window != null)
            {
                _window.SizeChanged -= MainWindowSizeChanged;
                _window.LocationChanged -= MainWindowLocationChanged;
                _window.StateChanged -= MainWindowStateChanged;
            }
        }


        private void RestorePosition()
        {
            if (_settingsManager.Settings.IsNewOne) return;


            if (_settingsManager.Settings.Width <= SystemParameters.WorkArea.Width)
                _window.Width = _settingsManager.Settings.Width;

            if (_settingsManager.Settings.Height <= SystemParameters.WorkArea.Height)
                _window.Height = _settingsManager.Settings.Height;

            var soatTheTop = (_settingsManager.Settings.Top + _window.Height) <= 60; //60 safey pix.;
            var soUnderGround = (_settingsManager.Settings.Top + _window.Height) >= SystemParameters.WorkArea.Height;
            if (!soatTheTop && !soUnderGround)
                _window.Top = _settingsManager.Settings.Top;

            var soLeft = (_settingsManager.Settings.Left + _window.Width) >= SystemParameters.WorkArea.Width;
            var soRight = (_settingsManager.Settings.Left + _window.Width) <= 60; //60 safey pix.
            if (!soLeft && !soRight)
                _window.Left = _settingsManager.Settings.Left;

            _window.WindowState = _settingsManager.Settings.WindowState;
            _window.WindowStartupLocation = WindowStartupLocation.Manual;
        }

        private void TrackWindowPosition()
        {
            _window.SizeChanged += MainWindowSizeChanged;
            _window.LocationChanged += MainWindowLocationChanged;
            _window.StateChanged += MainWindowStateChanged;
        }

        private void MainWindowLocationChanged(object sender, EventArgs e)
        {
            var top = _settingsManager.Settings.Top;
            var left = _settingsManager.Settings.Left;

            _settingsManager.Settings.Top = _window.Top;
            _settingsManager.Settings.Left = _window.Left;

            if (top != _settingsManager.Settings.Top ||
                left != _settingsManager.Settings.Left)
                _settingsManager.Persist();
        }

        private void MainWindowSizeChanged(object sender, SizeChangedEventArgs e)
        {
            var width = _settingsManager.Settings.Width;
            var height = _settingsManager.Settings.Height;

            _settingsManager.Settings.Width = _window.Width;
            _settingsManager.Settings.Height = _window.Height;

            if (width != _settingsManager.Settings.Width ||
                height != _settingsManager.Settings.Height)
                _settingsManager.Persist();
        }

        private void MainWindowStateChanged(object sender, EventArgs e)
        {
            _settingsManager.Settings.WindowState = _window.WindowState;
            //Non cool for the perf but at least if the app crashes, the last settings are keeped (not like in VS :-)
            _settingsManager.Persist();
        }
    }
}